<?php

/**
 * @desc admin page
 * @version $Id: 20120122
 * @author n0aX
 * @package Administration
 */

$admin_request = $tk->page->_pv['admin_request'];
$admin_page = $tk->page->_pv['admin_page'];

$tk->assign('admin_page', $admin_page);
$tk->assign('admin_request', $admin_request);

$item_table = USERS_TABLE;

/**
 * @ignore createtime,status
 */

$item_fields = "username,user_firstname,user_lastname,user_level,user_email,user_phone,user_fax";

/**
 * @desc item fields override
 */

$item_override = "user_id as id,user_status as status,user_regdate as createtime,";

/**
 * @uses text/imagebrowser/textarea/wysiwyg/hidden/filebrowser/select/date/checkbox/number/readonly_text
 */

$item_array = array(
		'user_id' => 'hidden', 
		'username' => 'text', 
		'user_plain_password' => 'text', 
		'user_firstname' => 'text', 
		'user_lastname' => 'text', 
		'user_email' => 'text', 
		'user_phone' => 'text', 
		'user_fax' => 'text', 
		
		'arr' => array());

$where_condition = '1';
$order_by = '';

$search_array = array(
		'id_only' => array(
				'id' => 'text'), 
		'time-interval' => array(
				'status' => 'select', 
				'starttime' => 'time', 
				'endtime' => 'time'), 
		'arr' => array());

$search_array['arr']['status'] = array(
		0 => $lang[$pagename]['status_t']['0'], 
		1 => $lang[$pagename]['status_t']['1'], 
		2 => $lang[$pagename]['status_t']['2']);
$search_array['arr']['status']['-1'] = $lang['all'];
$search_array['arr']['user']['-1'] = $lang['all'];
ksort($search_array['arr']['user']);
ksort($search_array['arr']['status']);


$tk->page->_pv['items'] = $item_array;

if ((int) $_POST['submit'] || $tk->page->request == 'json')
{	
	include (__PHPROOT__ . 'pages' . DS . 'admin' . DS . 'json_errors.php');
	
	if (is_file(__PHPROOT__ . 'pages' . DS . 'admin' . DS . 'ajax' . DS . 'ajax_' . $tk->page->_pv['admin_page'] . '.php'))
	{
		include (__PHPROOT__ . 'pages' . DS . 'admin' . DS . 'ajax' . DS . 'ajax_' . $tk->page->_pv['admin_page'] . '.php');
	}
	
	return;
}

switch ($admin_request)
{
	case 'add_user' :
	break;
	
	case 'import_users' :
	break;
	
	case 'browse_users' :
	default :
		
		if ((int) ($search_type['id_only']) && (int) $req['get']['user_id'])
		{
			$where_condition = "WHERE id = '{$req['get']['user_id']}'";
		}
		else
		{
			if ((int) $req['get']['s'])
			{
				
				$search_type = $req['get']['f'];
				foreach ( $search_array[$search_type] as $key => $value )
				{
					if ($req['get'][$key] != '-1')
					{
						if ($value == 'time' && $req['get'][$key] != '')
						{
							$req['get'][$key] = $tk->f->date2int($req['get'][$key]);
						}
						switch ($key)
						{
							case 'starttime' :
								// 2 weeks
								$req['get'][$key] = ($req['get'][$key] != '') ? $tk->f->date2int(date('d/m/Y', $req['get'][$key])) : ($tk->config['time'] - 1209600);
								$where_condition[] = "user_regdate >= '{$req['get'][$key]}'";
							break;
							case 'endtime' :
								$req['get'][$key] = ($req['get'][$key] != '') ? $tk->f->date2int(date('d/m/Y', $req['get'][$key])) : $tk->config['time'];
								$where_condition[] = "user_regdate <= '{$req['get'][$key]}'";
							break;
							default :
								$where_condition[] = "$key = '{$req['get'][$key]}'";
							break;
						}
					}
				}
				
				$where_condition = implode(" AND ", $where_condition);
			}
			
			$order_by = "ORDER BY username ASC";
		}
		
		$sql = "SELECT SQL_CACHE $item_override $item_fields from $item_table WHERE $where_condition $order_by";
		
		if (! ($result = $tk->db->sql_query($sql)))
		{
			$error = 'sql';
			$tk->eh->message_die($error, __LINE__, __FILE__, $sql);
			return;
		}
		$i = 0;
		while($row = $tk->db->sql_fetchrow($result))
		{
			$data['list'][$i] = $row;
			$data['list'][$i]['user_fullname'] = $row['user_firstname'] . ' ' . $row['user_surname'];
			$i++;
		}
	
	break;
}


